﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using System.Security;
using FredCK.FCKeditorV2;
using System.Threading;
using System.Globalization;
using BLL.Store;
using CKEditor.NET;

public partial class Admin_AddEditProduct : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (!string.IsNullOrEmpty(this.Request.QueryString["ID"]))
            {
                if (this.User.Identity.IsAuthenticated && (this.User.IsInRole("Administrators")))
                {
                    dvwProduct.ChangeMode(DetailsViewMode.Edit);


                    UpdateTitle();
                }
                else
                    throw new SecurityException("You are not allowed to edit existent products!");
            }
        }
        FileUpload2Control1.FolderUpload = "Products";
        FileUpload2Control1.ObjectControlMax = (dvwProduct.FindControl("txtFullImageUrl") as TextBox);
        FileUpload2Control1.ObjectControlMin = (dvwProduct.FindControl("txtSmallImageUrl") as TextBox);
    }
    private void UpdateTitle()
    {
        lblNewProduct.Visible = (dvwProduct.CurrentMode == DetailsViewMode.Insert);
        lblEditProduct.Visible = !lblNewProduct.Visible;
    }
    protected void dvwProduct_ModeChanged(object sender, EventArgs e)
    {
        UpdateTitle();
    }
    protected void dvwProduct_DataBound(object sender, EventArgs e)
    {
        if (dvwProduct.CurrentMode == DetailsViewMode.Insert)
        {
            //(dvwProduct.FindControl("txtQuantity") as TextBox).Text = "0";
            (dvwProduct.FindControl("txtDiscountPercentage") as TextBox).Text = "0";
            (dvwProduct.FindControl("txtUnitPrice") as TextBox).Text = "0";
        }

        if (dvwProduct.CurrentMode == DetailsViewMode.Edit)
        {
            if (!string.IsNullOrEmpty(this.Request.QueryString["ID"]))
            {                
                Product prduct = Product.GetProductByID(int.Parse(this.Request["ID"]));
                (dvwProduct.FindControl("txtUnitPrice") as TextBox).Text = prduct.RoundUnitPrice;
                (dvwProduct.FindControl("ddlDepartments1") as DropDownList).DataBind();
                if (prduct.DepartmentID > 0)
                {
                    (dvwProduct.FindControl("ddlDepartments") as DropDownList).SelectedValue = prduct.DepartmentID.ToString();
                    (dvwProduct.FindControl("ddlDepartments") as DropDownList).DataBind();
                }
            }
        }
    }
    protected void dvwProduct_ItemCreated(object sender, EventArgs e)
    {
        Control ctl = dvwProduct.FindControl("txtBody");
        if (ctl != null)
        {
            CKEditorControl txtBody = ctl as CKEditorControl;
            txtBody.Language = "vi";
            CKFinder.FileBrowser _FileBrowser = new CKFinder.FileBrowser();
            _FileBrowser.BasePath = "/ckfinder/";
            _FileBrowser.SetupCKEditor(txtBody);
        }
    }
    protected override void InitializeCulture()
    {
        Thread.CurrentThread.CurrentUICulture = new CultureInfo("vi-VN");
        Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("vi-VN");
    }
    protected void dvwProduct_DataBinding(object sender, EventArgs e)
    {
        
    }
    protected void dvwProduct_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
    {
        e.NewValues["departmentID"] = (dvwProduct.FindControl("ddlDepartments") as DropDownList).SelectedValue;
    }
    protected void dvwProduct_ItemInserting(object sender, DetailsViewInsertEventArgs e)
    {
        e.Values["departmentID"] = (dvwProduct.FindControl("ddlDepartments") as DropDownList).SelectedValue;
    }
    protected void ddlDepartments1_DataBound(object sender, EventArgs e)
    {
        ddlDepartments.DataBind();
    }
}
